#include"../mqserver/mq_message.hpp"
#include<gtest/gtest.h>

mq::MessageManager::ptr msgmp;

class MessageTest : public testing::Environment{
    public:
        virtual void SetUp() override{
            msgmp = std::make_shared<mq::MessageManager>("./a/");
            msgmp->initQMessage("message1");

            // std::cout<<"create file success"<<std::endl;
        }
        virtual void TearDown() override{
            // std::cout<<"clear..."<<std::endl;
            // msgmp->clear();
        }
};

// TEST(message_test,test_test){
//     msgmp->insert("message1",nullptr,"hello-world-5",mq::DeliveryMode::UNDURABLE);
//     ASSERT_EQ(msgmp->getableCount("message1"),5);
//     ASSERT_EQ(msgmp->totalCount("message1"),4);
//     ASSERT_EQ(msgmp->durableCount("message1"),4);
//     ASSERT_EQ(msgmp->waitackCount("message1"),0);
// }

// TEST(message_test,insert_test){
//     mq::BasicProperty property;
//     property.set_id(mq::Uuidhelper::BuideUuid());
//     property.set_deliverymod(mq::DeliveryMode::DURABLE);
//     property.set_routingkey("news.music.pop");

//     msgmp->insert("message1",&property,"hello-world-1",true);
//     msgmp->insert("message1",nullptr,"hello-world-2",true);
//     msgmp->insert("message1",nullptr,"hello-world-3",true);
//     msgmp->insert("message1",nullptr,"hello-world-4",true);
//     msgmp->insert("message1",nullptr,"hello-world-5",false);

//     ASSERT_EQ(msgmp->getableCount("message1"),5);
//     ASSERT_EQ(msgmp->totalCount("message1"),4);
//     ASSERT_EQ(msgmp->durableCount("message1"),4);
//     ASSERT_EQ(msgmp->waitackCount("message1"),0);

//     mq::MqMessagePtr msg1 = msgmp->front("message1");
//     ASSERT_EQ(msgmp->getableCount("message1"),4);
//     ASSERT_NE(msg1,nullptr);
//     ASSERT_EQ(msgmp->waitackCount("message1"),1);
//     ASSERT_EQ(msg1->payload().body(),"hello-world-1");
//     ASSERT_EQ(msg1->payload().basepro().routingkey(),"news.music.pop");
//     ASSERT_EQ(msg1->payload().basepro().deliverymod(),mq::DeliveryMode::DURABLE);
// }

// TEST(message_test,select_test){

//     ASSERT_EQ(msgmp->getableCount("message1"),4);
//     ASSERT_EQ(msgmp->totalCount("message1"),4);
//     ASSERT_EQ(msgmp->durableCount("message1"),4);
//     ASSERT_EQ(msgmp->waitackCount("message1"),0);

//     mq::MqMessagePtr msg1 = msgmp->front("message1");
//     ASSERT_EQ(msgmp->getableCount("message1"),3);
//     ASSERT_NE(msg1,nullptr);
//     ASSERT_EQ(msgmp->waitackCount("message1"),1);
//     ASSERT_EQ(msg1->payload().body(),"hello-world-1");
//     ASSERT_EQ(msg1->payload().basepro().routingkey(),"news.music.pop");
//     ASSERT_EQ(msg1->payload().basepro().deliverymod(),mq::DeliveryMode::DURABLE);

//     msgmp->ack("message1",msg1->payload().basepro().id());
//     ASSERT_EQ(msgmp->durableCount("message1"),3);

// }

TEST(message_test,test_test){
    msgmp->insert("message1",nullptr,"hello-world-5",false);
    ASSERT_EQ(msgmp->getableCount("message1"),4);
    ASSERT_EQ(msgmp->totalCount("message1"),3);
    ASSERT_EQ(msgmp->durableCount("message1"),3);
    ASSERT_EQ(msgmp->waitackCount("message1"),0);

    mq::MqMessagePtr msg1 = msgmp->front("message1");
    ASSERT_EQ(msgmp->getableCount("message1"),3);
    ASSERT_NE(msg1,nullptr);
    ASSERT_EQ(msgmp->waitackCount("message1"),1);
    ASSERT_EQ(msg1->payload().body(),"hello-world-2");
    ASSERT_EQ(msg1->payload().basepro().routingkey(),"");
    ASSERT_EQ(msg1->payload().basepro().deliverymod(),mq::DeliveryMode::DURABLE);

    msgmp->destroyQMessage("message1");
    ASSERT_EQ(msgmp->totalCount("message1"),0);
}

int main(int argc,char* argv[]){
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new MessageTest);
    return RUN_ALL_TESTS();
}
